﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Threading;
using System.Media;
using System.Reflection;
using FX.CC.Andon.EqKanban.KanbanDataSvcRef;
using System.IO;

namespace FX.CC.Andon.EqKanban
{
    public partial class frmTaskList : DevExpress.XtraEditors.XtraForm
    {
        IList<PrdTaskWrapper> dispList = new List<PrdTaskWrapper>();

        KanbanDataSvc svc;

        const int page_size = 5;

        int clickCount = 0;
        int restartClickCount = 0;

        System.Timers.Timer chkTimer;
        Action<IList<PrdTaskWrapper>> actSetGridDataSource;
        Action actUpdateTime;

        double hourOffset, minuteOffset;
        public frmTaskList()
        {
            InitializeComponent();
            this.gridControl2.Dock = DockStyle.Fill;
            this.Location = new Point(0, 0);
            double.TryParse(ConfigurationManager.AppSettings["HourOffset"], out hourOffset);
            double.TryParse(ConfigurationManager.AppSettings["MinOffset"], out minuteOffset);

            actSetGridDataSource = new Action<IList<PrdTaskWrapper>>(p =>
            {
                gridControl2.DataSource = p;
                gridView2.BestFitColumns();
            });

            actUpdateTime = new Action(() => lbTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分"));

            svc = new KanbanDataSvc();

            //同步时间
            try
            {
                var svrTime = svc.GetServerTime();
                _API.SetLocalTime(svrTime);
            }
            catch { }

            chkTimer = new System.Timers.Timer(60 * 1000);
            chkTimer.Elapsed += new System.Timers.ElapsedEventHandler(chkTimer_Elapsed);
            chkTimer.Start();
        }

        void chkTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            DateTime curTime = DateTime.Now;
            this.Invoke(actUpdateTime);

            if (curTime.Hour == 0 && curTime.Minute == 0) {
                ThreadPool.QueueUserWorkItem((obj) => {
                    try { 
                        string docPath = Application.StartupPath + "\\Doc";
                        var fileNameSet = System.IO.Directory.GetFiles(docPath);
                        foreach (var fileName in fileNameSet)
                        {
                            FileInfo fi = new FileInfo(fileName);
                            if (curTime.Subtract(fi.LastAccessTime).TotalDays > 7)
                            {
                                fi.Delete();
                            }
                        }
                    }
                    catch { }
                });
            }

            DateTime realBizDate = curTime.AddHours(-1 * hourOffset).AddMinutes(-1 * minuteOffset).Date;
            try
            {
                var taskSet = svc.GetTaskList(Program.bindingEq.EqID, realBizDate);
                var taskList = taskSet.ToList();
                this.BeginInvoke(actSetGridDataSource, taskList);
            }
            catch
            {

            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            lbTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分");
            lbVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            var taskSet = svc.GetTaskList(Program.bindingEq.EqID, DateTime.Now.Date);
            gridControl2.DataSource = taskSet;
            gridView2.BestFitColumns();
        }

        private void lbVersion_DoubleClick(object sender, EventArgs e)
        {
            if (++clickCount >= 5)
            {
                clickCount = 0;
                this.Close();
            }
        }

        private void gridView2_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {

        }

        private void gridView2_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (Program.cursorShown)
                {
                    Cursor.Hide();
                }
                else
                {
                    Cursor.Show();
                }
                Program.cursorShown = !Program.cursorShown;
            }
        }

        private void picLogo_DoubleClick(object sender, EventArgs e)
        {
            if (++restartClickCount >= 5)
            {
                restartClickCount = 0;
                if (_MsgBox.PopQuestion("是否重启看板控制器？") != DialogResult.Yes)
                {
                    return;
                }

                System.Diagnostics.Process.Start("Shutdown.exe", @" -r -t 0");
            }
        }

        private void lbTime_DoubleClick(object sender, EventArgs e)
        {
            //using (var frm = new frmConfig()) {
            //    frm.ShowDialog();
            //}
        }

        private void frmTaskList_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                chkTimer.Stop();
                chkTimer.Dispose();
            }
            catch { }
        }

    }
}
